package gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import client.ComplaintDetails;
import client.LogIn;
import client.Main;
import client.MainScreen;

	public class FileComplaint_GUI extends JFrame implements ActionListener, TableModelListener{
		
		public String[] choice={"non-handled","handled"};
		public String sn[]=new String[3];  
		String complaint = null;
		String userID;
		public MainScreen mainScreen = null;
		public Object[][] data = null;
		public String[] columnNames = {"Complaint ID","Complaint Description","Response"};
		
		//buttons
		public JButton btnBack;
		public JButton btnFileComplaint;
		public JButton btnLogOut;
		public JButton btnReviewComp;
		public JButton btnSubmit;
		public JButton btnSubmit1;
		public JButton btnBack1;
		public JButton btnBack2;
		public JButton btnBack3;

		//panels
		JPanel Mainpanel;
		JPanel panelFileComp;
		JPanel panelShowComp;
		JPanel panelTable;
		
		//text input
		JTextArea textInput;
		JTextArea textID;
		
		//labels
		JLabel lblHeader;
		JLabel lblHeaderFileComp;
		JLabel lblHeaderShowComp;
		JLabel lblHeaderTable;
		JLabel lblText;
		JLabel lblOption;
		JLabel lblID;

		//combo box
		JComboBox options;
		
		Container contentPane;
		
	
	public FileComplaint_GUI (){
			
			super();
			contentPane = this.getContentPane();
		    contentPane.setLayout(new BorderLayout(0, 0));
			this.setBounds((int)Main.dim.getWidth()/2-260, (int)Main.dim.getHeight()/2-275, 515, 550);
			this.setResizable(false);
			this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
			this.setTitle("Zer Lah - File Complaint");
			contentPane.add(getPanelFileComp(),BorderLayout.CENTER);
			this.setSize(500,400);
			this.setVisible(true);
		}
	
	
	JPanel getPanelFileComp() 
	{	
		if (panelFileComp == null){
			panelFileComp = new JPanel();
			panelFileComp.setSize(500, 400);
			panelFileComp.setLayout(null);
			panelFileComp.add(getBtnBack());
			panelFileComp.add(getBtnSubmit());
			panelFileComp.add(getLabelHeaderFileComp());
			panelFileComp.add(getTextInput());
			panelFileComp.add(getLblText());
			panelFileComp.add(getLblID());
			panelFileComp.add(getTextID());
			panelFileComp.add(getBtnLogOut());
		}
		return panelFileComp;
	}
	
	JPanel getPanelShowComp()
	{
		if (panelShowComp == null){
			panelShowComp = new JPanel();
			panelShowComp.setSize(500, 400);
			panelShowComp.setLayout(null);
			panelShowComp.add(getBtnBack2());
			panelShowComp.add(getBtnLogOut());
			panelShowComp.add(getCombo());
			panelShowComp.add(getLblOption());
			panelShowComp.add(getLabelShowComp());
			panelShowComp.add(getBtnSubmit1());
		}
		return panelShowComp;
	}
	
	JPanel getPanelTable()
	{
	if (panelTable == null)
		{
		panelTable = new JPanel();
		panelTable.setSize(1000, 400);
		panelTable.setLayout(null);
		panelTable.add(getTable());
		panelTable.add(getBtnBack3());
		panelTable.add(getLabelHeaderTable());
		panelTable.add(getBtnLogOut());
		}
		return panelTable;
	}
	
	JComboBox getCombo()
	{
		if(options==null)
		{
			options=new JComboBox();
			for(int i=0;i<2;i++)
				options.addItem(choice[i]);
				options.setBounds(280, 200, 150, 40);
		}
		return options;
	}
	
	JLabel getLblOption()
	{
		if(lblOption==null)
		{
			lblOption=new JLabel();
			lblOption.setText("Choose an option:");
			lblOption.setBounds(40, 200, 180, 40);
			lblOption.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		}
		return lblOption;
	}
	
	JLabel getLabelHeader()
	{
		if(lblHeader==null)
		{
			lblHeader=new JLabel();
			lblHeader.setText("Complaints");
			lblHeader.setBounds(150, 100, 180, 40);
			lblHeader.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,25));
		}
		return lblHeader;
	}
	
	JLabel getLabelHeaderTable()
	{
		if(lblHeaderTable==null)
		{
			lblHeaderTable=new JLabel();
			lblHeaderTable.setText("Complaints List");
			lblHeaderTable.setBounds(360, 100, 180, 40);
			lblHeaderTable.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,25));
		}
		return lblHeaderTable;
	}
	
	JLabel getLabelShowComp()
	{
		if(lblHeaderShowComp==null)
		{
			lblHeaderShowComp=new JLabel();
			lblHeaderShowComp.setText("My Complaints");
			lblHeaderShowComp.setBounds(150, 100, 180, 40);
			lblHeaderShowComp.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,25));
		}
		return lblHeaderShowComp;
	}
	
	JLabel getLabelHeaderFileComp()
	{
		if(lblHeaderFileComp==null)
		{
			lblHeaderFileComp=new JLabel();
			lblHeaderFileComp.setText("File Complaint");
			lblHeaderFileComp.setBounds(150, 70, 180, 40);
			lblHeaderFileComp.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,25));
		}
		return lblHeaderFileComp;
	}
	
	JLabel getLblText()
	{
		if(lblText==null)
		{
			lblText=new JLabel();
			lblText.setText("Type your complaint:");
			lblText.setBounds(40, 180, 180, 40);
			lblText.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		}
		return lblText;
	}
	
	JLabel getLblID()
	{
		if(lblID==null)
		{
			lblID=new JLabel();
			lblID.setText("Insert User ID:");
			lblID.setBounds(40, 125, 180, 40);
			lblID.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		}
		return lblID;
	}
	
		
	JButton getBtnBack(){
			
			if (btnBack == null){
				btnBack = new JButton();
				btnBack.setBounds(380, 20, 100, 40);
				btnBack.setText("Back");
			}
			return btnBack;
		}
	
	JButton getBtnBack1(){
		
			if (btnBack1 == null){
				btnBack1 = new JButton();
				btnBack1.setBounds(380, 20, 100, 40);
				btnBack1.setText("Back");
				btnBack1.addActionListener(this);
			}
			return btnBack1;
		}
	
	JButton getBtnBack2(){
			
			if (btnBack2 == null){
				btnBack2 = new JButton();
				btnBack2.setBounds(380, 20, 100, 40);
				btnBack2.setText("Back");
				btnBack2.addActionListener(this);
			}
			return btnBack2;
		}

	JButton getBtnBack3(){
		
			if (btnBack3 == null){
				btnBack3 = new JButton();
				btnBack3.setBounds(880, 20, 100, 40);
				btnBack3.setText("Back");
				btnBack3.addActionListener(this);
			}
			return btnBack3;
		}
	
	JButton getBtnFileComplaint(){
		
			if (btnFileComplaint == null){
				btnFileComplaint = new JButton();
				btnFileComplaint.setBounds(300, 250, 150, 40);
				btnFileComplaint.setText("File Complaint");
				btnFileComplaint.addActionListener(this);
			}
			return btnFileComplaint;
		}
	
	JButton getBtnReviewOldComp(){
			
			if (btnReviewComp == null){
				btnReviewComp = new JButton();
				btnReviewComp.setBounds(300, 300, 150, 40);
				btnReviewComp.setText("My Complaints");
				btnReviewComp.addActionListener(this);
			}
			return btnReviewComp;
		}
		
	JButton getBtnLogOut() {
			
			if (btnLogOut == null){
				btnLogOut = new JButton();
				btnLogOut.setBounds(20,20,100,40);
				btnLogOut.addActionListener(this);
				btnLogOut.setText("LogOut");
			}
			return btnLogOut;
		}
	
	JButton getBtnSubmit() {
		
			if (btnSubmit == null){
				btnSubmit = new JButton();
				btnSubmit.setBounds(150,315,200,40);
				btnSubmit.addActionListener(this);
				btnSubmit.setText("Submit");
			}
			return btnSubmit;
		}
	
	JButton getBtnSubmit1() {
		
		if (btnSubmit1 == null){
			btnSubmit1 = new JButton();
			btnSubmit1.setBounds(150,300,200,40);
			btnSubmit1.addActionListener(this);
			btnSubmit1.setText("Submit");
		}
		return btnSubmit1;
	}
	
	JTextArea getTextInput()
	{
		textInput= new JTextArea(60,1);
		textInput.setBounds(40,220,430,85);
		textInput.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		return textInput;
	}
	
	JTextArea getTextID()
	{
		textID= new JTextArea(60,1);
		textID.setBounds(320, 125, 150, 40);
		textID.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		return textID;
	}
	
	JScrollPane getTable(){
		
		  DefaultTableModel model = new DefaultTableModel(data, columnNames) {
		      public boolean isCellEditable(int rowIndex, int mColIndex) {
		      return false;
		      }
		  };
		  JTable table = new JTable(model); 
		  table.getModel().addTableModelListener(this);
		  TableColumn column = table.getColumnModel().getColumn(2);
		  column.setPreferredWidth(200);
		  column = table.getColumnModel().getColumn(0);
		  column.setPreferredWidth(1);
		  column = table.getColumnModel().getColumn(1);
		  column.setPreferredWidth(5);
	      JScrollPane scroll = new JScrollPane(table); 
	      scroll.setBounds(90, 150, 800, 190);
	      table.setBounds(90, 150, 800, 190);
	      return scroll; 
	}

	
	public void actionPerformed(ActionEvent e) {
		
			if(e.getSource() == btnLogOut)
			{
				if(JOptionPane.showConfirmDialog(null,"Are you sure you want to logout?",null,JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
				{
					this.setEnabled(false);
					this.setVisible(false);
					Main.main.logOut.doClick();
				}
			}
			
			if(e.getSource() == btnFileComplaint)
			{
				panelFileComp=null;
				this.setSize(500, 400);
				contentPane.removeAll();
				contentPane.add(getPanelFileComp(),BorderLayout.CENTER);
				validate();
				setVisible(true);
			}
			
			if(e.getSource() == btnSubmit)
			{
				
				
				if((textInput.getText().isEmpty()) || (textID.getText().isEmpty()))
					JOptionPane.showMessageDialog(null, "All fields are required!");
				else
				{
				complaint = textInput.getText();
				userID = textID.getText();
				int check = ComplaintDetails.setCompDetail(complaint,userID);
				if(check==0)
					JOptionPane.showMessageDialog(null, "User does not exist in the system!");
				else
				{	
				JOptionPane.showMessageDialog(null, "Compaint has been added successfully!");
				Mainpanel=null;
				this.getContentPane().removeAll();
				}
				btnBack.doClick();
				}
			}
			
			if(e.getSource() == btnReviewComp)
			{
				panelShowComp=null;
				this.getContentPane().removeAll();
				this.getContentPane().add(getPanelShowComp(),BorderLayout.CENTER);
				validate();
				setVisible(true);
			}
			
			if(e.getSource() == btnSubmit1)
			{
				panelShowComp=null;
				sn[0] = "get my complaints";
				sn[1] = (String) options.getSelectedItem();
				sn[2] = Integer.toString(LogIn.usr.getID());
				data = ComplaintDetails.getComplaintDetails(sn);
				this.getContentPane().removeAll();
				this.setSize(1000, 400);
				this.setBounds(150, 260, 1000, 400);
				this.getContentPane().add(getPanelTable(),BorderLayout.CENTER);
				validate();
				setVisible(true);
			}
	
			
		}

	
	public void tableChanged(TableModelEvent arg0) {
		
	}
}
